﻿/* 
 *  <copyright file="ModelCmp.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SobrietyEngine.Actor;
using SobrietyEngine.Common;

namespace SobrietyEngine.Components.Graphic
{
    public class ModelCmp : Base3DGraphic, SobrietyEngine.Components.IComponent
    {
        String modelAssetName;
        Model model;
        Matrix[] ModelBoneTransforms;

        IOrientation orientation;
        IPosition Position;
        IScale Scale;

        Vector3 scale = Vector3.One;
        Vector3 pos = new Vector3();
        Matrix rot = Matrix.Identity;
        Matrix world = Matrix.Identity;

        public ModelCmp()
            : base()
        {
        }

        public ModelCmp(Model _model)
            : base()
        {
            model = _model;
        }

        public ModelCmp(String _model)
            : base()
        {
            modelAssetName = _model;
        }

        public override void Set(Parameters parms)
        {
            base.Set(parms);

            modelAssetName = parms.GetValueAs<String>("ModelName", "none");           
        }

        public override List<ParameterDescription> GetParameterDescriptions()
        {
            List<ParameterDescription> p = base.GetParameterDescriptions();
            p.Add(new ParameterDescription("ModelName", "Model asset name", typeof(String), "none"));
            return p;
        }

         override public Boolean InitComponent()
        {
            base.InitComponent();

            if(model == null)
                model = Actor.GameServices().AssetGameService.Load<Model>(modelAssetName);

            if (modelAssetName == "none" || model == null)
                Logging.Logger.Error(this, "Missing modelAssetName / model"); 

            orientation = Actor.Get<IOrientation>();

            Position = Actor.Get<IPosition>();
            if (Position == null)
                throw new Exception("Position required");

            Scale = Actor.Get<IScale>();

            // Copy any parent ModelBoneTransforms.
            ModelBoneTransforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(ModelBoneTransforms);

            return true;
        }

        public override void UpdateGraphic(GameTime time)
        {
            if (orientation != null)
                rot = orientation.RotationMatrix;

            if (Scale != null)
                scale = Scale.Scale;

            pos = Position.PositionV3;
            world = Matrix.CreateScale(scale) * rot * Matrix.CreateTranslation(pos);            
        }

        public override void Draw(ICamera _camera, Matrix _projection, Matrix _view, Matrix _world)
        {
            if (Visible == false)
                return;

            // Draw the model. A model can have multiple meshes, so loop.
            foreach (ModelMesh mesh in model.Meshes)
            {
                // This is where the mesh Orientation is set, as well 
                // as our camera and projection.
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    //effect.World = ModelBoneTransforms[mesh.ParentBone.Index] * world;
                    effect.World = world * _world;
                    effect.View = _view;
                    effect.Projection = _projection;
                }
                // Draw the mesh, using the effects set above.
                mesh.Draw();
            }
        }
    }
}